day53-python之会话

from django.shortcuts import render,redirect

# Create your views here.
import datetime

def login(request):
    print("COOKIES",request.COOKIES)
    print("SESSION",request.session)

    if request.method=="POST":
        name=request.POST.get("user")
        pwd=request.POST.get("pwd")
        if name=="andy" and pwd=="1234":

            # ret=redirect("/index/")
            # ret.set_cookie("username",{"11":"22"},max_age=10,expires=datetime.datetime.utcnow()+datetime.timedelta(days=3))
            # return ret

            # COOKIE SESSION
            request.session["is_login"]=True
            request.session["user"]=name

            return redirect("/index/")

    return render(request,"login.html")

def index(request):

    if request.session.get("user",None):
        name = request.session.get("session",None)
        return render(request, "index.html", locals())


    # if request.session.get("is_login",None):
    #     name=request.session.get("user",None)
    #     return render(request,"index.html",locals())

    else:
        return redirect("/login/")

 

posted @ 2019-09-13 18:27  有猿人  阅读(277)  评论(0编辑  收藏  举报